package com.jumi.microservice.material.service.impl;

import com.jumi.microservice.common.redis.RedisCache;
import com.jumi.microservice.material.domain.JmWarehouseSeq;
import com.jumi.microservice.material.mapper.JmWarehouseSeqMapper;
import com.jumi.microservice.material.service.JmWarehouseSeqService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author ssk
 * @create 2020/7/22 11:46
 **/
@Service
public class JmWarehouseSeqServiceImpl implements JmWarehouseSeqService {

    @Resource
    private JmWarehouseSeqMapper jmWarehouseSeqMapper;
    @Resource
    private RedisCache redisCache;
    public static final String areaKey = "area:";
    @Override
    @Transactional
    public int save(JmWarehouseSeq jmWarehouseSeq) {
        Long[] areaIds = jmWarehouseSeq.getAreaIds();
        List<Long> usedAreas = jmWarehouseSeqMapper.findUsedArea();
        if (isExist(usedAreas, Arrays.asList(areaIds)))
            return 0;
        int saveRows = jmWarehouseSeqMapper.save(jmWarehouseSeq);
        jmWarehouseSeqMapper.saveLinkArea(jmWarehouseSeq);
        jmWarehouseSeqMapper.saveLinkWarehouse(jmWarehouseSeq);
        List<Map> byIdInsert = jmWarehouseSeqMapper.findByIdInsert(areaIds);
        for (Map n : byIdInsert) {
            double sort = Double.parseDouble(n.get("sort").toString());
            String warehouseId = n.get("warehouseId").toString();
            String area_id = n.get("area_id").toString();
            redisCache.setZset(areaKey+area_id,sort,warehouseId);
        }
        return saveRows;
    }

    /**
     * 验证是两集合是否存在交集
     *
     * @param oldList
     * @param newList
     * @return
     */
    private boolean isExist(List oldList, List newList) {
        if (oldList != null && oldList.size() > 0) {
            oldList.retainAll(newList);
            return !oldList.isEmpty();
        } else {
            return false;
        }

    }

    @Override
    @Transactional
    public int update(JmWarehouseSeq jmWarehouseSeq) {
        jmWarehouseSeqMapper.delSeqAreaBySeqId(jmWarehouseSeq.getSeqId());
        jmWarehouseSeqMapper.delSeqWarehouseBySeqId(jmWarehouseSeq.getSeqId());
        Long[] areaIds = jmWarehouseSeq.getAreaIds();
        List<Long> usedAreas = jmWarehouseSeqMapper.findUsedArea();
        if (isExist(usedAreas, Arrays.asList(areaIds)))
            return 0;
        int updateRows = jmWarehouseSeqMapper.update(jmWarehouseSeq);
        jmWarehouseSeqMapper.saveLinkArea(jmWarehouseSeq);
        jmWarehouseSeqMapper.saveLinkWarehouse(jmWarehouseSeq);

        List<Map> byIdInsert = jmWarehouseSeqMapper.findByIdInsert(areaIds);
        for (Map n : byIdInsert) {
            String area_id = n.get("area_id").toString();
            boolean isCaches = redisCache.hasObjectByKey(areaKey+area_id);
            if(isCaches){
                redisCache.deleteObject(areaKey+area_id);
            }
            double sort = Double.parseDouble(n.get("sort").toString());
            String warehouseId = n.get("warehouseId").toString();
            redisCache.setZset(areaKey+area_id,sort,warehouseId);
        }
        return updateRows;
    }

    @Override
    public JmWarehouseSeq findById(Long seqId) {
        JmWarehouseSeq jmWarehouseSeq = jmWarehouseSeqMapper.findById(seqId);
        fillAreaAndWarehouse(jmWarehouseSeq);
        return jmWarehouseSeq;
    }

    @Override
    public JmWarehouseSeq findById2(Long seqId) {
        JmWarehouseSeq jmWarehouseSeq = jmWarehouseSeqMapper.findById2(seqId);
        return jmWarehouseSeq;
    }

    private void fillAreaAndWarehouse(JmWarehouseSeq jmWarehouseSeq) {
        if (jmWarehouseSeq != null) {
            Long[] areaIds = jmWarehouseSeqMapper.findAreaIds(jmWarehouseSeq.getSeqId());
            if (areaIds != null && areaIds.length > 0) {
                List<Map> areas = jmWarehouseSeqMapper.findAreaByAreaIds(areaIds);
                jmWarehouseSeq.setAreas(areas);
            }
            List<Map> warehouses = jmWarehouseSeqMapper.findWarehouseBySeqId(jmWarehouseSeq.getSeqId());
            jmWarehouseSeq.setWarehouses(warehouses);
        }
    }

    @Override
    public List<JmWarehouseSeq> findAll(JmWarehouseSeq jmWarehouseSeq) {

        List<JmWarehouseSeq> list = jmWarehouseSeqMapper.findAll(jmWarehouseSeq);
        for (JmWarehouseSeq seq : list) {
            fillAreaAndWarehouse(seq);
        }
        return list;
    }

    @Override
    @Transactional
    public List<JmWarehouseSeq> findAll2(JmWarehouseSeq jmWarehouseSeq) {

        Set seqIdSet=new HashSet();

        String areaName=jmWarehouseSeq.getAreaName();

        if(StringUtils.isNotBlank(areaName)){
            List<Long> areaSeqIds=jmWarehouseSeqMapper.findSeqIdsByArea(areaName);
            if(areaSeqIds.size()>0){
                seqIdSet.addAll(areaSeqIds);
            }else {
                return new ArrayList<>();
            }

        }
        String warehouseName=jmWarehouseSeq.getWarehouseName();

        if(StringUtils.isNotBlank(warehouseName)){
            List<Long> warehouseSeqIds=jmWarehouseSeqMapper.findSeqIdsByWarehouse(warehouseName);
            if(warehouseSeqIds.size()>0){
                if(seqIdSet.size()>0)
                    seqIdSet.retainAll(warehouseSeqIds);
                else
                    seqIdSet.addAll(warehouseSeqIds);
            }else {
                return new ArrayList<>();
            }
        }
        if(seqIdSet.size()>0)
            jmWarehouseSeq.setSeqIds(seqIdSet);
        List<JmWarehouseSeq> list= jmWarehouseSeqMapper.findAll2(jmWarehouseSeq);
        return list;
    }

    @Override
    @Transactional
    public int delSeqBySeqId(Long seqId) {
        int delRows = jmWarehouseSeqMapper.delSeqBySeqId(seqId);
        jmWarehouseSeqMapper.delSeqAreaBySeqId(seqId);
        jmWarehouseSeqMapper.delSeqWarehouseBySeqId(seqId);
        return delRows;
    }

    @Override
    public Set findSeqIdsByParam(JmWarehouseSeq jmWarehouseSeq) {
        List list=new ArrayList();
        if(StringUtils.isNotBlank(jmWarehouseSeq.getAreaName())){
            Long[] areaIds= jmWarehouseSeqMapper.findAreaIdsByName(jmWarehouseSeq.getAreaName());
            if (areaIds != null && areaIds.length > 0) {
                List<Long> areaSeqIds = jmWarehouseSeqMapper.findSeqIdsByAreaIds(areaIds);
                list.addAll(areaSeqIds);
            }
        }
        if(StringUtils.isNotBlank(jmWarehouseSeq.getWarehouseName())){
            List<Long> warehouseSeqIds= jmWarehouseSeqMapper.findSeqIdsByWarehouse(jmWarehouseSeq.getWarehouseName());
            list.addAll(warehouseSeqIds);
        }
        if(list.size()>0){
            Set set= new HashSet(list);
            return set;
        }
        return null;
    }

    @Override
    public int updateStatus(JmWarehouseSeq jmWarehouseSeq) {
        int updateRows = jmWarehouseSeqMapper.update(jmWarehouseSeq);
        return updateRows;
    }

    @Override
    public List<Map<String, Object>> findByAreaId(Long areaId) {
        List<Map<String, Object>> byAreaId = jmWarehouseSeqMapper.findByAreaId(areaId);
        return jmWarehouseSeqMapper.findByAreaId(areaId);
    }


}
